//
//  LoginWindow.m
//  ALS3.0
//
//  Created by guomianxiang on 17/3/13.
//  Copyright © 2017年 guomianxiang. All rights reserved.
//

#import "LoginWindow.h"
#import <SVProgressHUD.h>
#import "ALSHeader.h"

@implementation LoginWindow

+ (LoginWindow *)shareLoginWindw {
    static LoginWindow *loginWindow = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        loginWindow = [[LoginWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        loginWindow.windowLevel = 100 ;
    });
    return loginWindow;
}

- (void)show{
//#warning 删除
//    [NSUD setBool:YES forKey:ALSAdministrators];
//    [self dismiss];
//    return;

    [self endEditing:YES];
    
    UIStoryboard *Login = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    self.rootViewController = [Login instantiateInitialViewController];
    self.windowLevel = 100;
    [self makeKeyWindow];
    self.hidden = NO;
    
    [[NSNotificationCenter defaultCenter]postNotificationName:LoginWindowShowNotification object:nil];
}

- (void)dismiss {
    [self endEditing:YES];

    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0.01;
    } completion:^(BOOL finished) {
        self.hidden = YES;
        self.alpha = 1;
        
        [[NSNotificationCenter defaultCenter]postNotificationName:LoginWindowDismissNotification object:nil];
    }];
    
}

- (void)showSetTouchIDLoginHUD {
    [self endEditing:YES];
    
    self.windowLevel = UIWindowLevelNormal;
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD showErrorWithStatus:@"指纹识别暂不可用，请在“设置”打开“指纹登录”后即可使用"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        self.windowLevel = 100;
        
    });}
- (void)showTouchIDLoadingHUD {
    [self endEditing:YES];
    self.windowLevel = UIWindowLevelNormal;
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD showWithStatus:@"指纹登陆"];
    
}
- (void)showTouchIDLockHUD {
    [self endEditing:YES];

    self.windowLevel = UIWindowLevelNormal;
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD showErrorWithStatus:@"指纹识别暂不可用，使用密码解锁iPhone后即可恢复"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        self.windowLevel = 100;
        
    });
}
- (void)showTouchIDAuthenticationFailedHUD {
    [self endEditing:YES];

    self.windowLevel = UIWindowLevelNormal;
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD showErrorWithStatus:@"指纹验证失败"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        self.windowLevel = 100;
        
    });
}
- (void)showMessageAdminHUD {
    [self endEditing:YES];
    
    self.windowLevel = UIWindowLevelNormal;
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD showInfoWithStatus:@"请联系管理员"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        self.windowLevel = 100;
    });

}

- (void)showAdminLoginSuccessHUDAndDismiss {
    [self endEditing:YES];
    
    self.windowLevel = UIWindowLevelNormal;
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD showSuccessWithStatus:@"管理员登录成功"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        self.windowLevel = 100;
        [self dismiss];
    });

}
- (void)showSetNewPasswordSuccessHUDAndDismiss {
    [self endEditing:YES];
    
    self.windowLevel = UIWindowLevelNormal;
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD showSuccessWithStatus:@"密码修改成功"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        self.windowLevel = 100;
        [self dismiss];
    });
}

@end

NSNotificationName const LoginWindowShowNotification = @"LoginWindowShowNotification";

NSNotificationName const LoginWindowDismissNotification = @"LoginWindowDismissNotification";
